# Before `make install' is performed this script should be runnable with
# `make test'. After `make install' it should work as `perl Binary-Tree.t'

#########################

# change 'tests => 1' to 'tests => last_test_to_print';

use strict;
use warnings;

use Test::More tests => '44';
use Test::Fatal;
$| = 1;
BEGIN { use_ok('Binary::Tree') };

use Scalar::Util 'isweak';

my $root = Binary::Tree->new( node => 'root');
isa_ok($root, 'Binary::Tree');

is($root->node, 'root', '... got the right node value');

ok(!$root->has_left, '... no left node yet');
ok(!$root->has_right, '... no right node yet');

ok(!$root->has_parent, '... no parent for root node');

# make a left node

my $left = $root->left;
isa_ok($left, 'Binary::Tree');

is($root->left, $left, '... got the same node (and it is $left)');
ok($root->has_left, '... we have a left node now');

ok($left->has_parent, '... lefts has a parent');
is($left->parent, $root, '... lefts parent is the root');

ok(isweak($left->{parent}), '... parent is a weakened ref');

ok(!$left->has_left, '... $left no left node yet');
ok(!$left->has_right, '... $left no right node yet');

is($left->node, undef, '... left has got no node value');

is(
    exception {
        $left->node('left');
    },
    undef,
    '... assign to lefts node'
);

is($left->node, 'left', '... left now has a node value');

# make a right node

ok(!$root->has_right, '... still no right node yet');

is($root->right->node, undef, '... right has got no node value');

ok($root->has_right, '... now we have a right node');

my $right = $root->right;
isa_ok($right, 'Binary::Tree');

is(
    exception {
        $right->node('right');
    },
    undef,
    '... assign to rights node'
);

is($right->node, 'right', '... left now has a node value');

is($root->right, $right, '... got the same node (and it is $right)');
ok($root->has_right, '... we have a right node now');

ok($right->has_parent, '... rights has a parent');
is($right->parent, $root, '... rights parent is the root');

ok(isweak($right->{parent}), '... parent is a weakened ref');

# make inorder list from tree
my @refs;
is(
    exception {
        @refs = $root->traverse;
    },
    undef,
    '... created a inorder list from tree'
);


my @inorder = (\$left, \$root, \$right);
is(
  $root->traverse,
  @inorder,
  '... inorder list well built'
);

# make a left node of the left node

my $left_left = $left->left;
isa_ok($left_left, 'Binary::Tree');

ok($left_left->has_parent, '... left does have a parent');

is($left_left->parent, $left, '... got a parent node (and it is $left)');
ok($left->has_left, '... we have a left node now');
is($left->left, $left_left, '... got a left node (and it is $left_left)');

ok(isweak($left_left->{parent}), '... parent is a weakened ref');

# make a right node of the left node

my $left_right = Binary::Tree->new;
isa_ok($left_right, 'Binary::Tree');

is(
    exception {
        $left->right($left_right);
    },
    undef,
    '... assign to rights node'
);

ok($left_right->has_parent, '... left does have a parent');

is($left_right->parent, $left, '... got a parent node (and it is $left)');
ok($left->has_right, '... we have a left node now');
is($left->right, $left_right, '... got a left node (and it is $left_left)');

ok(isweak($left_right->{parent}), '... parent is a weakened ref');

# and check the error

isnt(
    exception {
        $left_right->right($left_left);
    },
    undef,
    '... cannot assign a node which already has a parent'
);

#########################

# Insert your test code below, the Test::More module is use()ed here so read
# its man page ( perldoc Test::More ) for help writing this test script.

